<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="转载自 https:&#x2F;&#x2F;blog.lqdev.cn&#x2F;2019&#x2F;05&#x2F;08&#x2F;springboot&#x2F;chapter-thirty-seven&#x2F;  前言 近期在进行项目安全方面评审时，质量管理部门有提出需要对配置文件中的敏高文件进行加密处理，避免了信息泄露问题。想想前段时间某公司上传github时，把相应的生产数据库明文密码也一并上传了，导致了相应的数据泄露问题。也确实，大部分项目无论开发、测试还是生">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot集成jasypt实现配置加密">
<meta property="og:url" content="http://littlefxc.github.io/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="转载自 https:&#x2F;&#x2F;blog.lqdev.cn&#x2F;2019&#x2F;05&#x2F;08&#x2F;springboot&#x2F;chapter-thirty-seven&#x2F;  前言 近期在进行项目安全方面评审时，质量管理部门有提出需要对配置文件中的敏高文件进行加密处理，避免了信息泄露问题。想想前段时间某公司上传github时，把相应的生产数据库明文密码也一并上传了，导致了相应的数据泄露问题。也确实，大部分项目无论开发、测试还是生">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/WTvOjzNR7Xt5.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/bPoPUpE8uk4A.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/568wfkAWA5Tl.png">
<meta property="article:published_time" content="2021-06-02T11:32:08.000Z">
<meta property="article:modified_time" content="2021-06-02T11:46:56.000Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="SpringBoot">
<meta property="article:tag" content="jasypt">
<meta property="article:tag" content="加密">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/WTvOjzNR7Xt5.png">


<link rel="canonical" href="http://littlefxc.github.io/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>SpringBoot集成jasypt实现配置加密 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E7%82%B9%E7%9F%A5%E8%AF%86"><span class="nav-text">一点知识</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%95%E4%B8%BAJasypt"><span class="nav-text">何为Jasypt</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SpringBoot%E9%9B%86%E6%88%90Jasypt"><span class="nav-text">SpringBoot集成Jasypt</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E8%A7%84%E9%9B%86%E6%88%90%E7%A4%BA%E4%BE%8B"><span class="nav-text">常规集成示例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%85%E5%90%ABxml%E5%BC%95%E5%85%A5%E6%97%B6"><span class="nav-text">包含xml引入时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E9%85%8D%E7%BD%AE%E9%A1%B9"><span class="nav-text">其他配置项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%90%E7%BB%B4%E8%AF%B4%E6%98%8E"><span class="nav-text">运维说明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AF%86%E9%92%A5%EF%BC%88%E7%9B%90%E5%80%BC%EF%BC%89%E5%AD%98%E5%82%A8%E8%AF%B4%E6%98%8E"><span class="nav-text">密钥（盐值）存储说明</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E7%A4%BA%E4%BE%8B"><span class="nav-text">命令行示例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E7%A4%BA%E4%BE%8B"><span class="nav-text">环境变量示例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bat%E8%84%9A%E6%9C%AC"><span class="nav-text">bat脚本</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="nav-text">参考资料</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          SpringBoot集成jasypt实现配置加密
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-02 19:32:08 / 修改时间：19:46:56" itemprop="dateCreated datePublished" datetime="2021-06-02T19:32:08+08:00">2021-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/java-spring/" itemprop="url" rel="index"><span itemprop="name">java/spring</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/">https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/</a></p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><blockquote>
<p>近期在进行项目安全方面评审时，质量管理部门有提出需要对配置文件中的敏高文件进行加密处理，避免了信息泄露问题。想想前段时间某公司上传github时，把相应的生产数据库明文密码也一并上传了，导致了相应的数据泄露问题。也确实，大部分项目无论开发、测试还是生产环境，相关的敏高信息都是明文存储的，也是一大安全隐患呀。所以今天来说说，如何对配置文件进行加密操作。</p>
</blockquote>
<h2 id="一点知识"><a href="#一点知识" class="headerlink" title="一点知识"></a>一点知识</h2><h3 id="何为Jasypt"><a href="#何为Jasypt" class="headerlink" title="何为Jasypt"></a>何为Jasypt</h3><blockquote>
<p><a target="_blank" rel="noopener" href="http://jasypt.org/">Jasypt</a>是一个Java库，允许开发人员以很简单的方式添加基本加密功能，而无需深入研究加密原理。利用它可以实现高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件。</p>
</blockquote>
<ol>
<li>高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件…</li>
<li>集成Hibernate的。</li>
<li>可集成到Spring应用程序中，与Spring Security集成。</li>
<li>集成的能力，用于加密的应用程序（即数据源）的配置。</li>
<li>特定功能的高性能加密的multi-processor/multi-core系统。</li>
<li>与任何JCE提供者使用开放的API</li>
</ol>
<p>官网：<a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></p>
<h2 id="SpringBoot集成Jasypt"><a href="#SpringBoot集成Jasypt" class="headerlink" title="SpringBoot集成Jasypt"></a>SpringBoot集成Jasypt</h2><blockquote>
<p><code>SpringBoot</code>中集成<code>Jasypt</code>，可直接使用开源的<code>jasypt-spring-boot</code>直接集成，使用简单方便。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/WTvOjzNR7Xt5.png" alt="mark"></p>
<h3 id="常规集成示例"><a href="#常规集成示例" class="headerlink" title="常规集成示例"></a>常规集成示例</h3><ol>
<li>引入pom依赖</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>设置盐值和修改相应需要加密的配置参数</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 需要解密的地方，使用ENC()进行包裹处理</span><br><span class="line">okong.name&#x3D;ENC(Xj7Ykn2O0Hni&#x2F;tN4oojPfw&#x3D;&#x3D;)</span><br><span class="line"></span><br><span class="line"># 设置盐值，生产环境中，切记不要直接进行设置，可通过环境变量、命令行等形式进行设置。</span><br><span class="line">jasypt.encryptor.password&#x3D;lqdev</span><br></pre></td></tr></table></figure>

<p>简单来说，就是在需要加密的值使用<code>ENC(</code>和<code>)</code>进行包裹，即：<code>ENC(密文)</code>。若想避免参数冲突，可修改前缀和后缀，可以直接使用<code>jasypt.encryptor.property.prefix</code>和<code>jasypt.encryptor.property.suffix</code>进行修改即可。</p>
<p><strong>之后想往常一样使用<code>@Value(&quot;$&#123;&#125;&quot;)</code>即可。</strong></p>
<h3 id="包含xml引入时"><a href="#包含xml引入时" class="headerlink" title="包含xml引入时"></a>包含xml引入时</h3><blockquote>
<p>在一些使用<code>javaBean</code>配置和<code>xml</code>两种混合模式时，使用第一种配置时，<code>xml</code>参数并未替换。此时看了官方文档，可以使用另一方式进行配置即可。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/bPoPUpE8uk4A.png" alt="官方说明"></p>
<ol>
<li><p>引入pom依赖</p>
 <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p> 其实就是不进行自动配置而已。</p>
</li>
<li><p>启动类启动方式修改。</p>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JasyptApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SpringApplication.run(JasyptApplication.class, args);</span></span><br><span class="line">    <span class="comment">// 使用自定义环境变量 实现一些特殊场景下的加密字符解密操作</span></span><br><span class="line">    <span class="comment">// 若无额外的xml引入文件需要解密时，可直接使用SpringApplication.run(JasyptApplication.class, args);即可</span></span><br><span class="line">    <span class="comment">// 若想在引入的xml中使用，需要加入环境变量，如以下模式</span></span><br><span class="line">    <span class="keyword">new</span> SpringApplicationBuilder().environment(<span class="keyword">new</span> StandardEncryptableEnvironment())</span><br><span class="line">    .sources(JasyptApplication.class).run(args);</span><br><span class="line">    log.info(<span class="string">&quot;spring-boot-jasypt-chapter37服务启动!&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="其他配置项"><a href="#其他配置项" class="headerlink" title="其他配置项"></a>其他配置项</h3><table>
<thead>
<tr>
<th>Key</th>
<th>Required</th>
<th>Default Value</th>
</tr>
</thead>
<tbody><tr>
<td>jasypt.encryptor.password</td>
<td><strong>True</strong></td>
<td>盐值，根密码</td>
</tr>
<tr>
<td>jasypt.encryptor.algorithm</td>
<td>False</td>
<td>PBEWithMD5AndDES</td>
</tr>
<tr>
<td>jasypt.encryptor.keyObtentionIterations</td>
<td>False</td>
<td>1000</td>
</tr>
<tr>
<td>jasypt.encryptor.poolSize</td>
<td>False</td>
<td>1</td>
</tr>
<tr>
<td>jasypt.encryptor.providerName</td>
<td>False</td>
<td>SunJCE</td>
</tr>
<tr>
<td>jasypt.encryptor.providerClassName</td>
<td>False</td>
<td>null</td>
</tr>
<tr>
<td>jasypt.encryptor.saltGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.RandomSaltGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.ivGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.NoOpIVGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.stringOutputType</td>
<td>False</td>
<td>base64</td>
</tr>
<tr>
<td>jasypt.encryptor.proxyPropertySources</td>
<td>False</td>
<td>false</td>
</tr>
</tbody></table>
<h2 id="运维说明"><a href="#运维说明" class="headerlink" title="运维说明"></a>运维说明</h2><p>为了方便运维人员对各类敏感密钥进行加密操作，提供了自动化脚本，方便生成相应的加密串。</p>
<h3 id="密钥（盐值）存储说明"><a href="#密钥（盐值）存储说明" class="headerlink" title="密钥（盐值）存储说明"></a>密钥（盐值）存储说明</h3><p>本身加解密过程都是通过<code>盐值</code>进行处理的，所以正常情况下<code>盐值</code>和<code>加密串</code>是分开存储的。**<code>盐值</code>应该放在<code>系统属性</code>、<code>命令行</code>或是<code>环境变量</code>来使用，而不是放在配置文件。**</p>
<h4 id="命令行示例"><a href="#命令行示例" class="headerlink" title="命令行示例"></a>命令行示例</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=xxx &amp;</span><br></pre></td></tr></table></figure>

<h4 id="环境变量示例"><a href="#环境变量示例" class="headerlink" title="环境变量示例"></a>环境变量示例</h4><p>设置环境变量：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/profileexport JASYPT_PASSWORD = xxxx</span><br></pre></td></tr></table></figure>

<p>启动命令：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=<span class="variable">$&#123;JASYPT_PASSWORD&#125;</span> &amp;</span><br></pre></td></tr></table></figure>

<h3 id="bat脚本"><a href="#bat脚本" class="headerlink" title="bat脚本"></a>bat脚本</h3><p>为了方便，简单编写了一个bat脚本方便使用。</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">set</span>/p input=待加密的明文字符串：</span><br><span class="line"><span class="built_in">set</span>/p password=加密密钥(盐值)：</span><br><span class="line"><span class="built_in">echo</span> 加密中......</span><br><span class="line">java -cp jasypt-<span class="number">1</span>.<span class="number">9</span>.<span class="number">2</span>.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI  input=<span class="variable">%input%</span> password=<span class="variable">%password%</span> algorithm=PBEWithMD5AndDES</span><br><span class="line"><span class="built_in">pause</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：<code>jasypt-1.9.2.jar</code> 文件需要和bat脚本放在相同目录下。此包可直接在示例项目中直接下载。</strong></p>
<p>使用示例：</p>
<p><strong>注意：相应加密串，每次加密的结果是不同的。</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/568wfkAWA5Tl.png" alt="使用示例"></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本章节主要简单介绍了如何使用<code>jasypt</code>对配置文件进行加密操作。一些其他高级应用，可以查看官方文档进行相关集成即可。集成起来相对来说比较简单，注意是要对<code>密码(盐值)</code>的管理，需要进行安全把控下，建议运维人员针对每个项目进行不一样的盐值操作，避免一个项目泄露了，造成其他关联项的信息泄露。安全无大小呀，还是谨慎为妙！</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ol>
<li><a target="_blank" rel="noopener" href="https://github.com/ulisesbocchio/jasypt-spring-boot">https://github.com/ulisesbocchio/jasypt-spring-boot</a></li>
<li><a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/SpringBoot/" rel="tag"># SpringBoot</a>
              <a href="/tags/jasypt/" rel="tag"># jasypt</a>
              <a href="/tags/%E5%8A%A0%E5%AF%86/" rel="tag"># 加密</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" rel="prev" title="MyCat概述与基本概念">
                  <i class="fa fa-chevron-left"></i> MyCat概述与基本概念
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/06/02/%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86%E3%80%81%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86/" rel="next" title="如何选择垂直切分、水平切分">
                  如何选择垂直切分、水平切分 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
